﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BetterWebBooking
{
    public partial class Book : System.Web.UI.Page
    {
        private static BetterServiceReference.IBookingService bookObj = new BetterServiceReference.BookingServiceClient();

        //string username = HttpContext.Current.User.ToString();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FromStationDropDownList.DataSource = bookObj.GetAllStations();
                FromStationDropDownList.DataTextField = "StationName";
                FromStationDropDownList.DataValueField = "StationId";
                FromStationDropDownList.DataBind();
                ToStationDropDownList.DataSource = bookObj.GetAllStations();
                ToStationDropDownList.DataTextField = "StationName";
                ToStationDropDownList.DataValueField = "StationId";
                ToStationDropDownList.DataBind();

                DayDropDown.DataSource = Enumerable.Range(1, 31);
                DayDropDown.SelectedIndex = DateTime.Now.Day;
                DayDropDown.DataBind();
                
                MonthDropDown.SelectedIndex = DateTime.Now.Month;
                MonthDropDown.DataSource = Enumerable.Range(1, 12)
                    .Select(monthIndex =>
        CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthIndex))
    .ToArray();
                MonthDropDown.SelectedIndex = DateTime.Now.Month-1;

                MonthDropDown.DataBind();
                YearDropDown.DataSource = Enumerable.Range(DateTime.Now.Year, 5);
                YearDropDown.DataBind();

                HourDropDown.DataSource = Enumerable.Range(00, 23);
                HourDropDown.SelectedIndex = DateTime.Now.Hour;
                HourDropDown.DataBind();
                MinuteDropDown.DataSource = Enumerable.Range(00, 59);
                MinuteDropDown.SelectedIndex = DateTime.Now.Minute;
                MinuteDropDown.DataBind();
            }

            if (Session["User"] != null)
            {
                BetterServiceReference.Customer myobj = bookObj.GetCustomerByUserName(HttpContext.Current.Session["User"].ToString());
                SessionUserLabel.Text = myobj.Name;

            } 


        }

        protected void FindRouteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                IList<BetterServiceReference.Station> _stations = bookObj.GetAllStations();
                BetterServiceReference.Station _from = _stations[FromStationDropDownList.SelectedIndex];
                BetterServiceReference.Station _to = _stations[ToStationDropDownList.SelectedIndex];
                BetterServiceReference.Customer _customer = bookObj.GetCustomerByUserName(HttpContext.Current.Session["User"].ToString());
                Session["Customer"] = _customer;
                DateTime theDate = new DateTime(Convert.ToInt32(YearDropDown.SelectedValue), Convert.ToInt32(MonthDropDown.SelectedIndex+1), Convert.ToInt32(DayDropDown.SelectedValue), Convert.ToInt32(HourDropDown.SelectedValue), Convert.ToInt32(MinuteDropDown.SelectedValue), 0);
                IList<BetterServiceReference.Subscription> _subscriptions = bookObj.GetSubscriptions(_customer.CustomerId);
                BetterServiceReference.Subscription thisSub = _subscriptions[0];
                Session["Subscriptions"] = thisSub;
                //RouteListBox.DataSource = bookObj.CalculateRoute(_from.ZipCode, _from.City, _from.StationAddress, true, DateTime.Now, _to.ZipCode, _to.City, _to.StationAddress, _customer, _subscriptions[0]);
                //RouteListBox.DataBind();
                BetterServiceReference.Route _route = bookObj.CalculateRoute(_from.ZipCode, _from.City, _from.StationAddress, true, theDate, _to.ZipCode, _to.City, _to.StationAddress, _customer, _subscriptions[0]);
                Session["Route"] = _route;
                RouteTxtArea.Text = ("Du skal køre fra " + _route.FromCity + "\t til: " + _route.ToCity + "\n\n");
                int index = 1;
                foreach (BetterServiceReference.Booking booking in _route.Bookings)
                {
                    RouteTxtArea.Text += ("Bookingdato og tid for det " + index + ". batteriskifte: " + booking.BookingDate + "\n");
                    index++;

                    foreach (BetterServiceReference.Station station in _stations)
                    {
                        foreach (BetterServiceReference.Battery battery in station.Batteries)
                        {
                            if (battery.BatteryId == booking.Battery.BatteryId)
                            {
                                RouteTxtArea.Text += ("Batteriskifte bookes i:\n" + station.StationName + "\n" + station.StationAddress + "\n" + station.ZipCode + " " + station.City + "\n\n");
                            }
                        }
                    }
                } 
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert(\"Husk at logge ud og logge ind igen for at gemme bruger i Session Dato skal være i fremtiden\")</script>");
            }
        }

        protected void BookRouteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                bookObj.AcceptRoute((BetterServiceReference.Route)Session["Route"], (BetterServiceReference.Customer)Session["Customer"], (BetterServiceReference.Subscription)Session["Subscriptions"]);
                RouteTxtArea.Text += "Booking oprettet";
            }
            catch (Exception ex)
            {
                Response.Write(@"<script language='javascript'>alert('The following errors have occurred: \n" + ex.Message + " .');</script>");
            }
        }

    }
}